<template>
  <div>
    <!--  -->
     <el-row  v-loading="loading">
     <el-col :span="12">
    <el-form ref="elForm" :model="formData" 
    :rules="rules" 
    size="medium"
    label-suffix="："
      class="form-body site-form"
     label-width="160px">

      <h3 style="margin-top:0" class="form-section-title">基本信息</h3>

<!--  -->
      <el-form-item :label="typename+'名称'" prop="name">
        <el-input v-model="formData.name" placeholder="请输入名称" :maxlength="50"  
          prefix-icon='el-icon-mouse' :style="{width: '100%'}"></el-input>
      </el-form-item>
<!--  -->
      <el-form-item :label="typename+'域名'" prop="domain">
        <el-input v-model="formData.domain" placeholder="请输入域名" :style="{width: '50%'}"></el-input>
         <!--  -->
         <el-button type="warning" size="mini" >验证域名</el-button>

      </el-form-item>
<!--  -->
      <el-form-item :label="typename+'描述'" prop="smalltext">
        <el-input v-model="formData.smalltext" type="textarea" placeholder="请输入网站描述"
          :autosize="{minRows: 4, maxRows: 4}" :style="{width: '100%'}"></el-input>
        </el-form-item>
<!--  -->
      <el-form-item :label="typename+'类别'" prop="industry">
        <el-cascader v-model="formData.industry" :options="industryOptions" :props="industryProps"
          :style="{width: '50%'}" placeholder="请选择类别" clearable></el-cascader>
      </el-form-item>
<!--  -->
      <el-form-item label="访问量" prop="visitors">
        <el-select v-model="formData.visitors" placeholder="请选择访问量" clearable :style="{width: '50%'}">
          <el-option v-for="(item, index) in visitorsOptions" :key="index" :label="item.label"
            :value="item.value" :disabled="item.disabled"></el-option>
        </el-select>
      </el-form-item>
<!--  -->
      <el-form-item label="备案信息" prop="beian">
        <el-input v-model="formData.beian" placeholder="请输入备案信息" clearable :style="{width: '60%'}">
        </el-input>
        <span class="input-tips"> 如：桂ICP证01892222号</span>
      </el-form-item>
      <!-- 表单的提交和重置面板 -->
      <el-form-item size="large" class="form-submit-panel">
        <el-button type="primary" @click="submitForm">提交</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
<!--  -->
     </el-col>
     </el-row>
     <!--  -->
  </div>
</template>
<script>
import industryOptions from "./Industry.js";
import API from './SiteAPI.js';
import Options from './Options.js';

export default {
  components: {},
  props: ['addtype','idsite'],
  data() {
    return {      
      loading: false,
      formData: {
        name: '',
        domain: '',
        smalltext: '',
        industry: [],
        visitors: '',
        beian: '',
        //添加的类型
        addtype: this.addtype,
      },
      rules: {
        name: [{
          required: true,
          message: '请输入名称',
          trigger: 'blur'
        } ],
        domain: [{
          required: true,
          message: '请输入域名',
          trigger: 'blur'
        }],
        smalltext: [{
          required: true,
          message: '请输入描述',
          trigger: 'blur'
        }],
        industry: [{
          required: true,
          type: 'array',
          message: '请至少选择一个类别',
          trigger: 'change'
        }],
        visitors: [],
        beian: [],
      },
      industryOptions:  industryOptions,
      visitorsOptions:  Options.visitorsOptions,
      industryProps: {
        "multiple": false
      },
    }
  },
  computed: {
    typename(){
      if(this.addtype ==1){
        return '网站';
      }
      return '应用';
    },
    // 修改时候的站点id 通过组件 的属性值传入
    id(){
       return this.idsite? this.idsite: 0
    },
  },
  watch: {},
  created() {
    const that = this;
    if(this.id>0){
        this.loading = true;
        // 编辑站点信息
        API.getSiteFromId( this.id ) .then(data =>{
          that.loading = false;
          that.formData['name'] = data.name;
          that.formData['domain'] = data.main_url.split("//")[1];
          that.formData['industry'] = data.sitesearch_category_parameters.split('/');
          if(data.meta) {
              var meta =  JSON.parse(data.meta);
              that.formData['smalltext'] = meta.smalltext;
              that.formData['visitors'] =  meta.visitors ;
              that.formData['beian'] = meta.beian;
          }
        })
    }
  },
  mounted() {},
  methods: {
    submitForm() {
      if(this.loading){
        return; 
        // 防止重复提交
      }
      this.$refs['elForm'].validate(valid => {
        if (!valid) { return }
        // TODO 提交表单  
          this.loading = true;   
          if(this.id >0 ){
              API.updateSite(this.id,  this).then( data =>{
                  this.$message({ message: data.msg, type: "success" });
                  this.resetForm();
                  this.loading = false;
                  this.$emit('onclosedrawer', {from: 'submit'})
            });
          } else {
            API.addSite( this).then( data =>{
                this.$message({ message: data.msg, type: "success" });
                this.resetForm();
                this.loading = false;
                this.$emit('onclosedrawer', {from: 'submit'})
           });
          }  
         
      })
    },
    resetForm() {
      this.$refs['elForm'].resetFields()
    },
    
    getIndustryOptions() {
      // TODO 发起请求获取数据
      this.industryOptions
    },
  }
}

</script>
<style>
</style>
